package com.okaixz.client.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.okaixz.client.mapper.TaskInfoMapper;
import com.okaixz.client.model.domain.TaskInfo;
import com.okaixz.client.model.domain.User;
import com.okaixz.client.model.dto.UserDto;
import com.okaixz.client.model.vo.QianDaoInfoVo;
import com.okaixz.client.service.ITaskInfoService;
import com.okaixz.client.service.ITokenService;
import com.okaixz.client.service.IUserService;
import com.okaixz.common.client.exception.BusinessException;
import com.okaixz.common.client.exception.CommonErrorCode;
import com.okaixz.common.constant.CommonConstants;
import com.okaixz.common.utils.QianDaoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdz
 * @since 2025-06-11
 */
@Service
public class TaskInfoServiceImpl extends ServiceImpl<TaskInfoMapper, TaskInfo> implements ITaskInfoService {


    @Autowired
    private ITokenService tokenService;
    @Autowired
    private IUserService userService;
    @Override
    public TaskInfo getEnableTaskInfoByIdAndClientName(Integer taskId, String clientName) {
        if (taskId == null||clientName== null) {
            return null;
        }
        return lambdaQuery()
                .eq(TaskInfo::getId, taskId)
                .eq(TaskInfo::getClientName, clientName)
                .eq(TaskInfo::getStatus, CommonConstants.STATUS_ENABLE)
                .one();
    }

    @Override
    public List<TaskInfo> getListByClientName(String clientName) {
        if (clientName== null) {
            return null;
        }

        //返回结果按orderNum降序排序
        return lambdaQuery()
                .eq(TaskInfo::getClientName, clientName)
                .eq(TaskInfo::getStatus, CommonConstants.STATUS_ENABLE)
                .eq(TaskInfo::getType, CommonConstants.TASK_TYPE_SHOW_TO_USER)
                .orderByDesc(TaskInfo::getOrderNum)
                .list();
    }

    @Override
    public List<TaskInfo> getKbTaskInfo(String clientName, String userId) {
        return null;
    }

    @Override
    public QianDaoInfoVo qianDaoTask(String clientName, String userId) {
        List<TaskInfo> list = getListByClientName(clientName);
        if (list != null) {
            list=list.subList(0,2);
        }
        UserDto userDto = userService.getUserDtoFormRedisOrDb(userId);
        if (userDto == null) {
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }
        Integer qiandao = userDto.getQiandao();
        qiandao = qiandao==null?0:qiandao;
        int qianDaoWeekDays = QianDaoUtil.getQianDaoWeekDays(qiandao);//获取一星期内连续签到天数
        boolean isQianDaoToday = QianDaoUtil.isQianDaoTodayForWeek(qiandao);


        QianDaoInfoVo qianDaoInfoVo = new QianDaoInfoVo();
        qianDaoInfoVo.setQiandaoDays(qianDaoWeekDays);
        qianDaoInfoVo.setQianDaoToday(isQianDaoToday?1:0);
        qianDaoInfoVo.setUserDto(userDto);
        qianDaoInfoVo.setList(list);
        return qianDaoInfoVo;
    }
    @Override
    @Transactional
    public QianDaoInfoVo qianDao(String clientName, String userId) {
        User user = userService.getUserFormRedisOrDb(userId);
        if (user == null) {
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }
        Integer qiandao = user.getQiandao();
        int qianDaoWeekDays = QianDaoUtil.getQianDaoWeekDays(qiandao);//获取一星期内连续签到天数
        boolean isQianDaoToday = QianDaoUtil.isQianDaoTodayForWeek(qiandao);

        if (isQianDaoToday) {
            throw new BusinessException(CommonErrorCode.USER_QIANDANO_TODAY);
        }
        qiandao = QianDaoUtil.qiandaoForWeek(qiandao);
        user.setQiandao(qiandao);

        TaskInfo task = getEnableTaskInfoByIdAndClientName(24, clientName);
        if (task == null) {
            throw new BusinessException(CommonErrorCode.OP_FAIL);
        }
        user.setRemainTimes(user.getRemainTimes()+task.getPlusValue());


        UserDto userDto=userService.updateUserInfoByUserId(user);
        qiandao = userDto.getQiandao();
        qianDaoWeekDays = QianDaoUtil.getQianDaoWeekDays(qiandao);//获取一星期内连续签到天数

        QianDaoInfoVo qianDaoInfoVo = new QianDaoInfoVo();
        qianDaoInfoVo.setQiandaoDays(qianDaoWeekDays);
        qianDaoInfoVo.setQianDaoToday(1);
        qianDaoInfoVo.setUserDto(userDto);

        return qianDaoInfoVo;
    }

    @Override
    public QianDaoInfoVo finishTask(String clientName, String userId, Integer taskId) {
        User user = userService.getUserFormRedisOrDb(userId);
        if (user == null) {
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }
        Integer qiandao = user.getQiandao();
        int qianDaoWeekDays = QianDaoUtil.getQianDaoWeekDays(qiandao);//获取一星期内连续签到天数

        TaskInfo task = getEnableTaskInfoByIdAndClientName(taskId, clientName);
        if (task == null) {
            throw new BusinessException(CommonErrorCode.OP_FAIL);
        }
        switch (taskId) {
            case 22:
                //签到三天
                int score3Days = QianDaoUtil.getScore3Days(qiandao);
                if (score3Days==1) {
                    //已领取
                    throw new BusinessException(CommonErrorCode.TASK_ALRADAY_GETSCORE);
                }
                if (qianDaoWeekDays>=3) {
                    user.setRemainTimes(user.getRemainTimes()+task.getPlusValue());//增加积分
                    qiandao= QianDaoUtil.setScore3Days(qiandao);//将已领取设置到qiandao中
                }else {
                    throw new BusinessException(CommonErrorCode.TASK_NOT_FINISH);
                }
                break;
            case 23:
                //签到七天
                int score7Days = QianDaoUtil.getScore7Days(qiandao);
                if (score7Days==1) {
                    //已领取
                    throw new BusinessException(CommonErrorCode.TASK_ALRADAY_GETSCORE);
                }
                if (qianDaoWeekDays>=7) {
                    user.setRemainTimes(user.getRemainTimes()+task.getPlusValue());//增加积分
                    qiandao= QianDaoUtil.setScore7Days(qiandao);//将已领取设置到qiandao中
                }else {
                    throw new BusinessException(CommonErrorCode.TASK_NOT_FINISH);
                }
                break;
        }
        user.setQiandao(qiandao);
        UserDto userDto=userService.updateUserInfoByUserId(user);

        QianDaoInfoVo qianDaoInfoVo = new QianDaoInfoVo();
        qianDaoInfoVo.setQiandaoDays(qianDaoWeekDays);
        boolean isQianDaoToday = QianDaoUtil.isQianDaoTodayForWeek(qiandao);
        qianDaoInfoVo.setQianDaoToday(isQianDaoToday?1:0);
        qianDaoInfoVo.setUserDto(userDto);

        return qianDaoInfoVo;
    }

    @Override
    public TaskInfo getEnableTaskInfoByClientNameAndType(Integer type, String clientName) {
        if (type == null||clientName== null) {
            return null;
        }
        return lambdaQuery()
                .eq(TaskInfo::getType, type)
                .eq(TaskInfo::getClientName, clientName)
                .eq(TaskInfo::getStatus, CommonConstants.STATUS_ENABLE)
                .one();
    }
}
